/*
 * ToolTipDelegate.cpp  0.4.0 / Nov 17, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "ToolTipDelegate.h"

#include "ComponentDelegate.h"

// ToolTipDelegate :: Static Native Calls. -------------------------------------

/*!
 * \brief Enables/disables the tooltips.
 *
 * \param env the JNIEnv pointer
 * \param cls the ToolTip class
 * \param enabled \c true if enabled
 */
void ToolTipDelegate::setEnabled(JNIEnv *env, jclass cls, jboolean enabled)
{
  Q_UNUSED(env);
  Q_UNUSED(cls);
  sm_enabled = enabled;
}

/*!
 * \brief Sets font for tooltips.
 *
 * \param env the JNIEnv pointer
 * \param cls the ToolTip class
 * \param font the font
 */
void ToolTipDelegate::setFont(JNIEnv *env, jclass cls, jobject font)
{
  Q_ASSERT(env);
  SWING_STATIC_REENTRANT(ToolTipDelegate, setFont, SWING_ENV_ARG,
                         JCLASS_ARG(cls), JOBJECT_ARG(font))
  if (font) {
    QToolTip::setFont(*QFontAdapter::toQFont(font));
  } else {
    QToolTip::setFont(QFont());
  }
}

/*!
 * \brief Sets background color of the tooltips.
 *
 * \param env the JNIEnv pointer
 * \param cls the ToolTip class
 * \param argb the argb value of the color
 */
void ToolTipDelegate::setBackground(JNIEnv *env, jclass cls, jint argb)
{
  Q_ASSERT(env);
  SWING_STATIC_REENTRANT(ToolTipDelegate, setBackground, SWING_ENV_ARG,
                         JCLASS_ARG(cls), JINT_ARG(argb))
  QColorAdapter color(argb);
  QPalette palette(QToolTip::palette());
  if (color.isValid()) {
    palette.setColor(QPalette::Window, color);
  } else {
    palette.setColor(QPalette::Window,
                     QApplication::palette().color(QPalette::Window));
  }
  QToolTip::setPalette(palette);
}

/*!
 * \brief Sets default background color for tooltips.
 *
 * \param env the JNIEnv pointer
 * \param cls the ToolTip class
 */
void ToolTipDelegate::setNullBackground(JNIEnv *env, jclass cls)
{
  Q_ASSERT(env);
  SWING_STATIC_REENTRANT(ToolTipDelegate, setNullBackground, SWING_ENV_ARG,
                         JCLASS_ARG(cls))
  QPalette palette(QToolTip::palette());
  palette.setColor(QPalette::Window,
                   QApplication::palette().color(QPalette::Light));
  QToolTip::setPalette(palette);
}

/*!
 * \brief Sets foreground color of the tooltips.
 *
 * \param env the JNIEnv pointer
 * \param cls the ToolTip class
 * \param argb the argb value of the color
 */
void ToolTipDelegate::setForeground(JNIEnv *env, jclass cls, jint argb)
{
  Q_ASSERT(env);
  SWING_STATIC_REENTRANT(ToolTipDelegate, setForeground, SWING_ENV_ARG,
                         JCLASS_ARG(cls), JINT_ARG(argb))
  QColorAdapter color(argb);
  QPalette palette(QToolTip::palette());
  if (color.isValid()) {
    palette.setColor(QPalette::WindowText, color);
  } else {
    palette.setColor(QPalette::WindowText,
                     QApplication::palette().color(QPalette::WindowText));
  }
  QToolTip::setPalette(palette);
}

/*!
 * \brief Sets default foreground color for tooltips.
 *
 * \param env the JNIEnv pointer
 * \param cls the ToolTip class
 */
void ToolTipDelegate::setNullForeground(JNIEnv *env, jclass cls)
{
  Q_ASSERT(env);
  SWING_STATIC_REENTRANT(ToolTipDelegate, setNullForeground, SWING_ENV_ARG,
                         JCLASS_ARG(cls))
  QPalette palette(QToolTip::palette());
  palette.setColor(QPalette::WindowText,
                   QApplication::palette().color(QPalette::WindowText));
  QToolTip::setPalette(palette);
}

/*!
 * \brief Displays tooltip for the specified component.
 *
 * \param env the JNIEnv pointer
 * \param cls the ToolTip class
 * \param text the tooltip text
 * \param x the x-coordinate of the tooltip
 * \param y the y-coordinate of the tooltip
 * \param componentDelegate the component delegate pointer
 */
void ToolTipDelegate::showText(JNIEnv *env, jclass cls, jstring text, jint x,
                               jint y, jlong componentDelegate)
{
  Q_ASSERT(env);
  SWING_STATIC_REENTRANT(ToolTipDelegate, showText, SWING_ENV_ARG,
                         JCLASS_ARG(cls), JSTRING_ARG(text), JINT_ARG(x),
                         JINT_ARG(y), JLONG_ARG(componentDelegate))
  if (sm_enabled) {
    QWidget *widget = 0;
    if (componentDelegate) {
      widget = COMPONENT_CAST(componentDelegate)->qWidget();
      QToolTip::showText(widget->mapToGlobal(QPoint(x, y)), SwingString(text),
                         widget);
    } else {
      QToolTip::showText(QPoint(x, y), SwingString(text));
    }
  }
}

/*!
 * \brief Hides tooltip.
 *
 * \param env the JNIEnv pointer
 * \param cls the ToolTip class
 */
void ToolTipDelegate::hideText(JNIEnv *env, jclass cls)
{
  Q_ASSERT(env);
  SWING_STATIC_REENTRANT(ToolTipDelegate, hideText, SWING_ENV_ARG,
                         JCLASS_ARG(cls))
  if (sm_enabled) {
    QToolTip::hideText();
  }
}

// ToolTipDelegate :: Private Static Data Member. ------------------------------

bool ToolTipDelegate::sm_enabled = true;
